package com.example.team.service.Imp;

import com.example.team.dao.IVehicleDao;
import com.example.team.entity.Vehicle;
import com.example.team.service.IVehicleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * 对表格的定义规范
 * 编写者：蓝江富
 * 时间：2023年10月29日20:10:16
 */
//服务接口规范的方法实现
@Service
public class IVehicleServiceImpl implements IVehicleService {

    @Autowired
    private IVehicleDao vehicleMapper;
    @Override
    public int insertVehicle(Vehicle vehicle) {
        //设置成功状态
        int vehicleValue;
        Vehicle vehicle1=new Vehicle();
        vehicle1.setVehicleId(vehicle.getVehicleId());
        Vehicle allVehicle=vehicleMapper.getVehicleById(vehicle1);
        if(allVehicle!=null){
            vehicleValue = -1;
        }else {
            vehicleValue= vehicleMapper.insertVehicle(vehicle);
        }
        //返回状态码
        return vehicleValue;
    }

    @Override
    public int deleteVehicle(Vehicle vehicle) {
        //设置成功状态
        int vehicleValue;
        Vehicle vehicle1=new Vehicle();
        vehicle1.setLicense(vehicle.getLicense());
        List<Vehicle> allVehicle=vehicleMapper.getAllVehicle(vehicle1);
        if(allVehicle.size()==0){
            vehicleValue = -1;
        }else {
            vehicleValue= vehicleMapper.deleteVehicle(vehicle);
        }
        //返回状态码
        return vehicleValue;
    }

    @Override
    public int updateVehicle(Vehicle vehicle) {
        //设置成功状态
        int vehicleValue;
        //通过主键id对数据更新，判断是否存在目标id
        Vehicle vehicle1=new Vehicle();
        vehicle1.setVehicleId(vehicle.getVehicleId());
        Vehicle allVehicle=vehicleMapper.getVehicleById(vehicle1);
        if(allVehicle==null){
            //如果为空，说明无法找到匹配的目标Id，设置状态码-1
            vehicleValue = -1;
        }else {
            //否则执行更新数据库功能，同时赋值更新成功的数据数量到状态码
            vehicleValue= vehicleMapper.updateVehicle(vehicle);
        }
        //返回状态码
        return vehicleValue;
    }

    @Override
    public List<Vehicle> getAllVehicle(Vehicle vehicle) {
        return vehicleMapper.getAllVehicle(vehicle);
    }

    @Override
    public Vehicle getVehicle(Vehicle vehicle) {
        return vehicleMapper.getVehicle(vehicle);
    }

    //计算距离保修日期的时间
    @Override
    public String reminder(Vehicle vehicle) {
        //获取当前时间
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date localTime=new Date(System.currentTimeMillis());
        try {
            localTime=simpleDateFormat.parse(simpleDateFormat.format(localTime));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        //获取数据库数据的保修时间
        Date vehicleW=vehicleMapper.getVehicle(vehicle).getWarrantyDate();
        //获取两个时间的差距
        long difSecondLong=vehicleW.getTime()-localTime.getTime();
        int difS=(int)(difSecondLong/1000);
        int yy=difS/31536000;
        int remain=difS%31536000;
        int mM=remain/2592000;
        remain%=2592000;
        int dd=remain/86400;
        remain%=86400;
        int hh=remain/3600;
        remain%=3600;
        int mm=remain/60;
        int ss=remain%60;
        return vehicleMapper.getVehicle(vehicle).getWarrantyDate()+"\n"
                +yy+"/"+mM+"/"+dd+" "+hh+":"+mm+":"+ss;
    }
}
